package servletAction;

import java.io.IOException;
import java.util.HashSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Set;

import dao.DAOContactGroup;
import domain.Contact;

/**
 * Servlet implementation class NewContactGroup
 */
public class NewContactGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public NewContactGroup() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String groupName=request.getParameter("groupName");
		System.out.println("HELLO I'AM IN POST");
		if (groupName == ""){
			response.sendRedirect("addGroup.jsp");
			return;
		}
		// verifier que le nom du group n'existe pas encore
		try{
			//appel au DAO pour creation d'un nouveau group et forward vers une page d'ajout de contact
			DAOContactGroup dao = new DAOContactGroup();
			Contact c = new Contact();
			HashSet<Contact> cc = new HashSet<Contact>();
			cc.add(c);
			if (dao.addContactGroup(groupName,cc)){
				RequestDispatcher rd = request.getRequestDispatcher("/addContactInGroup.jsp");
				rd.forward(request, response);
			}else{
				response.sendRedirect("addGroup.jsp");
				return;
			}
		}catch (Exception e) { 
			response.sendRedirect("addGroup.jsp");
			return;
		}
	}

}
